#coding=utf-8
import arcpy
import json

from sympy import false

"""
多面--拆解为多个polygon
"""
# arcpy.env.overwriteOutput = True
# spatial_ref = arcpy.SpatialReference(4326)
# with open((r'D:\WorkData\20251118甘孜河流流域面\1.json').decode('utf-8'),'rb') as file:
#     content = file.read()
# data = json.loads(content)
# dataCount = len(data['features'])
# ArcpyPolyline_List = []
# for i in range(dataCount):
#     if len(data['features'][i]['geometry']['data']) > 0:
#         geo = json.loads(data['features'][i]['geometry']['data'][0]['geom'])['coordinates']
#         if len(geo) > 1:
#             for j in range(len(geo)):
#                 coordsList_Top = json.loads(data['features'][i]['geometry']['data'][0]['geom'])['coordinates'][j]
#
#                 ly = data['features'][i]['geometry']['data'][0]['sxname']
#                 name = data['features'][i]['geometry']['data'][0]['rname']
#                 pcdm = data['features'][i]['geometry']['data'][0]['pcdm'] or 'no_data'
#                 area = data['features'][i]['geometry']['data'][0]['basinarea']or 'no_data'
#                 print i,name
#                 for coordsList in coordsList_Top:
#                     ArcpyPoint_List = [arcpy.Point(*coords) for coords in coordsList]
#                     ArcpyArray_List = arcpy.Array(ArcpyPoint_List)
#                     ArcpyPolyline = arcpy.Polygon(ArcpyArray_List, spatial_ref)
#                     ArcpyPolyline_List.append([ArcpyPolyline, ly,name,pcdm,area])
#
#
# c_polyline = arcpy.CreateFeatureclass_management(r'D:\WorkData\20251118甘孜河流流域面'.decode('utf-8'), '1118A.shp', 'POLYGON',spatial_reference=spatial_ref)
# arcpy.AddField_management(c_polyline, 'ly', 'TEXT')
# arcpy.AddField_management(c_polyline, 'name', 'TEXT')
# arcpy.AddField_management(c_polyline, 'pcdm', 'TEXT')
# arcpy.AddField_management(c_polyline, 'area', 'TEXT')
# cs = arcpy.da.InsertCursor(c_polyline, ['SHAPE@', 'ly','name','pcdm','area'])
# for ArcpyPolyline in ArcpyPolyline_List:
#     cs.insertRow(ArcpyPolyline)
# del cs

"""
多面--直接生成multipolygon
"""
arcpy.env.overwriteOutput = True
spatial_ref = arcpy.SpatialReference(4326)
with open((r'D:\WorkData\20251118甘孜河流流域面\1.json').decode('utf-8'), 'rb') as file:
    content = file.read()
data = json.loads(content)
dataCount = len(data['features'])
ArcpyPolyline_List = []
for i in range(dataCount):
    if len(data['features'][i]['geometry']['data']) > 0:
        geo = json.loads(data['features'][i]['geometry']['data'][0]['geom'])['coordinates']
        FinalList = arcpy.Array()
        if len(geo) > 1:
            for j in range(len(geo)):
                coordsList_Top = json.loads(data['features'][i]['geometry']['data'][0]['geom'])['coordinates'][j]

                ly = data['features'][i]['geometry']['data'][0]['sxname']
                name = data['features'][i]['geometry']['data'][0]['rname']
                pcdm = data['features'][i]['geometry']['data'][0]['pcdm'] or 'no_data'
                area = data['features'][i]['geometry']['data'][0]['basinarea'] or 'no_data'
                print i, name

                for coordsList in coordsList_Top:
                    ArcpyPoint_List = [arcpy.Point(*coords) for coords in coordsList]
                    ArcpyArray_List = arcpy.Array(ArcpyPoint_List)
                    FinalList.append(ArcpyPoint_List)
            ArcpyPolyline = arcpy.Polygon(FinalList, spatial_ref)
            ArcpyPolyline_List.append([ArcpyPolyline, ly, name, pcdm, area])

c_polyline = arcpy.CreateFeatureclass_management(r'D:\WorkData\20251118甘孜河流流域面'.decode('utf-8'), '1118T.shp',
                                                 'POLYGON', spatial_reference=spatial_ref)
arcpy.AddField_management(c_polyline, 'ly', 'TEXT')
arcpy.AddField_management(c_polyline, 'name', 'TEXT')
arcpy.AddField_management(c_polyline, 'pcdm', 'TEXT')
arcpy.AddField_management(c_polyline, 'area', 'TEXT')
cs = arcpy.da.InsertCursor(c_polyline, ['SHAPE@', 'ly', 'name', 'pcdm', 'area'])
for ArcpyPolyline in ArcpyPolyline_List:
    cs.insertRow(ArcpyPolyline)
del cs